import router from '@/router'
import { getToken } from '@/utils/auth'
import store from '@/store'

// 路由拦截器
const whiteList = ['/login']

router.beforeEach(async (to, from, next) => {
  const token = getToken()
  if (token) {
    
    let userinfo
    if (!store.state.user.userinfo) {
      const res = await store.dispatch('user/setAsyncUserInfo')
      userinfo = res
    } else {
      userinfo = store.state.user.userinfo
    }
  
    if(to.meta.perms){
      // 需要权限的路由页面验证
      if(userinfo.perms_list.includes(to.meta.perms)){
        next()
      }else{
        return
      }
    }

    next()
    // 可以做权限控制
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {})
